1
|
|
|
/*! |
2
|
|
|
* @name ElkArte Forum |
3
|
|
|
* @copyright ElkArte Forum contributors |
4
|
|
|
* @license BSD http://opensource.org/licenses/BSD-3-Clause |
5
|
|
|
* |
6
|
|
|
* @version 1.1 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This file contains javascript associated with the drafts auto function as it |
11
|
|
|
* relates to a plain text box (no sceditor invocation) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The constructor for the plain text box auto-saver |
16
|
|
|
* |
17
|
|
|
* @param {object} oOptions |
18
|
|
|
*/ |
19
|
|
|
function elk_DraftAutoSave(oOptions) |
20
|
|
|
{ |
21
|
|
|
this.opt = oOptions; |
22
|
|
|
this.bInDraftMode = false; |
23
|
|
|
this.sCurDraftId = ''; |
24
|
|
|
this.oCurDraftDiv = null; |
25
|
|
|
this.interval_id = null; |
26
|
|
|
this.oDraftHandle = document.forms.postmodify["message"]; |
|
|
|
|
27
|
|
|
this.sLastSaved = ''; |
28
|
|
|
this.bCheckDraft = false; |
29
|
|
|
|
30
|
|
|
window.addEventListener("load", this.init.bind(this)); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Start our self calling routine |
35
|
|
|
*/ |
36
|
|
|
elk_DraftAutoSave.prototype.init = function() |
37
|
|
|
{ |
38
|
|
|
if (this.opt.iFreq > 0) |
39
|
|
|
{ |
40
|
|
|
// Start the autosaver timer |
41
|
|
|
this.interval_id = setInterval(this.draftSave.bind(this), this.opt.iFreq); |
42
|
|
|
|
43
|
|
|
// Set up the text area events |
44
|
|
|
this.oDraftHandle.onblur = this.draftBlur.bind(this); |
45
|
|
|
this.oDraftHandle.onfocus = this.draftFocus.bind(this); |
46
|
|
|
this.oDraftHandle.onkeydown = function(oEvent) { |
47
|
|
|
// Don't let tabbing to the buttons trigger autosave event |
48
|
|
|
if (oEvent.keyCode === 9) |
49
|
|
|
this.bInDraftMode = true; |
50
|
|
|
|
51
|
|
|
return this.draftKeypress().bind(this); |
52
|
|
|
}.bind(this); |
53
|
|
|
|
54
|
|
|
// Prevent autosave when selecting post/save by mouse or keyboard |
55
|
|
|
var $_button = $('#postmodify').find('.button_submit'); |
56
|
|
|
$_button .on('mousedown', this, function() { |
57
|
|
|
this.bInDraftMode = true; |
58
|
|
|
}.bind(this)); |
59
|
|
|
$_button .on('onkeypress', this, function() { |
60
|
|
|
this.bInDraftMode = true; |
61
|
|
|
}.bind(this)); |
62
|
|
|
} |
63
|
|
|
}; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Moved away from the page, where did you go? ... till you return we pause autosaving |
67
|
|
|
* - Handles the Blur event |
68
|
|
|
*/ |
69
|
|
|
elk_DraftAutoSave.prototype.draftBlur = function() |
70
|
|
|
{ |
71
|
|
|
// Save if we are not already |
72
|
|
|
if (this.bInDraftMode !== true) |
73
|
|
|
this.draftSave(); |
74
|
|
|
else |
75
|
|
|
this.draftCancel(); |
76
|
|
|
|
77
|
|
|
if (this.interval_id !== "") |
78
|
|
|
window.clearInterval(this.interval_id); |
79
|
|
|
|
80
|
|
|
this.interval_id = ""; |
81
|
|
|
}; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Since your back we resume the autosave timer |
85
|
|
|
* - Handles the focus event |
86
|
|
|
*/ |
87
|
|
|
elk_DraftAutoSave.prototype.draftFocus = function() |
88
|
|
|
{ |
89
|
|
|
if (this.interval_id === "") |
90
|
|
|
this.interval_id = setInterval(this.draftSave.bind(this), this.opt.iFreq); |
91
|
|
|
}; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Since your back we resume the autosave timer |
95
|
|
|
* - Handles the keypress event |
96
|
|
|
*/ |
97
|
|
|
elk_DraftAutoSave.prototype.draftKeypress = function() |
98
|
|
|
{ |
99
|
|
|
this.bCheckDraft = true; |
100
|
|
|
}; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Makes the ajax call to save this draft in the background |
104
|
|
|
*/ |
105
|
|
|
elk_DraftAutoSave.prototype.draftSave = function() |
106
|
|
|
{ |
107
|
|
|
// Form submitted or nothing changed since the last save |
108
|
|
|
if (elk_formSubmitted || !this.bCheckDraft) |
|
|
|
|
109
|
|
|
return false; |
110
|
|
|
|
111
|
|
|
// Still saving the last one or other? |
112
|
|
|
if (this.bInDraftMode) |
113
|
|
|
this.draftCancel(); |
114
|
|
|
|
115
|
|
|
// Nothing to save? |
116
|
|
|
var sPostdata = document.forms.postmodify["message"].value; |
|
|
|
|
117
|
|
|
if (isEmptyText(sPostdata) || !('topic' in document.forms.postmodify.elements)) |
118
|
|
|
return false; |
119
|
|
|
|
120
|
|
|
// Flag that we are saving a draft |
121
|
|
|
document.getElementById('throbber').style.display = 'inline'; |
122
|
|
|
this.bInDraftMode = true; |
123
|
|
|
|
124
|
|
|
// Get the form elements that we want to save |
125
|
|
|
var aSections = [ |
126
|
|
|
'topic=' + parseInt(document.forms.postmodify.elements['topic'].value), |
|
|
|
|
127
|
|
|
'id_draft=' + (('id_draft' in document.forms.postmodify.elements) ? parseInt(document.forms.postmodify.elements['id_draft'].value) : 0), |
|
|
|
|
128
|
|
|
'subject=' + document.forms.postmodify['subject'].value.replace(/&#/g, "&#").php_urlencode(), |
|
|
|
|
129
|
|
|
'message=' + sPostdata.replace(/&#/g, "&#").php_urlencode(), |
130
|
|
|
'icon=' + document.forms.postmodify['icon'].value.replace(/&#/g, "&#").php_urlencode(), |
|
|
|
|
131
|
|
|
'save_draft=true', |
132
|
|
|
'autosave=true', |
133
|
|
|
elk_session_var + '=' + elk_session_id |
|
|
|
|
134
|
|
|
]; |
135
|
|
|
|
136
|
|
|
// Send in document for saving and hope for the best |
137
|
|
|
sendXMLDocument.call(this, elk_prepareScriptUrl(elk_scripturl) + "action=post2;board=" + this.opt.iBoard + ";xml", aSections.join("&"), this.onDraftDone); |
|
|
|
|
138
|
|
|
|
139
|
|
|
// Save the latest for compare |
140
|
|
|
this.bCheckDraft = false; |
|
|
|
|
141
|
|
|
}; |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Callback function of the XMLhttp request for saving the draft message |
145
|
|
|
* @param {object} XMLDoc |
146
|
|
|
*/ |
147
|
|
|
elk_DraftAutoSave.prototype.onDraftDone = function(XMLDoc) |
148
|
|
|
{ |
149
|
|
|
// If it is not valid then clean up |
150
|
|
|
if (!XMLDoc || !XMLDoc.getElementsByTagName('draft')[0]) |
151
|
|
|
return this.draftCancel(); |
152
|
|
|
|
153
|
|
|
// Grab the returned draft id and saved time from the response |
154
|
|
|
this.sCurDraftId = XMLDoc.getElementsByTagName('draft')[0].getAttribute('id'); |
155
|
|
|
this.sLastSaved = XMLDoc.getElementsByTagName('draft')[0].childNodes[0].nodeValue; |
156
|
|
|
|
157
|
|
|
// Update the form to show we finished, if the id is not set, then set it |
158
|
|
|
document.getElementById(this.opt.sLastID).value = this.sCurDraftId; |
159
|
|
|
this.oCurDraftDiv = document.getElementById(this.opt.sLastNote); |
160
|
|
|
this.oCurDraftDiv.innerHTML = this.sLastSaved; |
161
|
|
|
|
162
|
|
|
// thank you sir, may I have another |
163
|
|
|
this.bInDraftMode = false; |
164
|
|
|
document.getElementById('throbber').style.display = 'none'; |
|
|
|
|
165
|
|
|
}; |
166
|
|
|
|
167
|
|
|
// If another auto save came in with one still pending |
168
|
|
|
elk_DraftAutoSave.prototype.draftCancel = function() |
169
|
|
|
{ |
170
|
|
|
this.bInDraftMode = false; |
171
|
|
|
document.getElementById('throbber').style.display = 'none'; |
172
|
|
|
}; |
You can rewrite this statement in dot notation: